package com.hangzhou.xyj.db;

import android.content.Context;

import com.hangzhou.xyj.model.SimpleUserModel;
import com.hangzhou.xyj.utils.StringUtil;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by yaohuix on 2016/4/23  16:37
 * <p/>
 * 说明：用于环信 缓存 用户头像到数据库中
 */
public class SimpleUserDao {
    private Dao<SimpleUserModel, String> mDao;
    private DataHelper mHelper = null;

    public SimpleUserDao(Context context) {
        this.mHelper = OpenHelperManager.getHelper(context, DataHelper.class);
        try {
            this.mDao = mHelper.getDao(SimpleUserModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加或更新用户
     *
     * @param info
     */
    public void addSimpleUser(SimpleUserModel info) {
        if (info == null) return;
        try {
            mDao.createOrUpdate(info);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据用户id获取当前用户信息
     *
     * @param uid
     * @return
     */
    public SimpleUserModel getUser(String uid) {
        if (StringUtil.isBlank(uid)) return null;
        SimpleUserModel info = null;
        try {
            List<SimpleUserModel> userModels = mDao.queryBuilder().where().eq("_id", uid).query();
            if (userModels == null || userModels.isEmpty()) {
                return null;
        } else {
            info = userModels.get(0);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
        return info;
    }
}
